// 系统配置
import { xitongList } from '@/api/system/xitong'

const xitong = {
  state: {
    info: {
      bossName: '运油宝运营管理总平台',
      logo: '426',
      logoUrl: {
        url:'https://lg01sz.oss-cn-shenzhen.aliyuncs.com/20231005/927cc7234ae54bbdaa39a56716309afa.png'
      },
      logiPcUrl: {
        url:'https://lg01sz.oss-cn-shenzhen.aliyuncs.com/20230924/97e414da734a4ebabfc526acf0ce46cd.jpg'
      }
    }
  },

  // 获取配置
  getters: {
    GET_XITONG(state) {
      return state.info
    }
  },

  // 设置系统配置
  mutations: {
    SET_XITONG: (state, info) => {

      // 若值不存在，则走默认配置
      state.info = {
        bossName: info?.bossName || state.info.bossName,
        logo: info?.logo || state.info.logo,
        logoUrl: info?.logoUrl || state.info.logoUrl,
        logiPcUrl: info?.logiPcUrl || state.info.logiPcUrl,
      }
      localStorage.setItem('xitong', JSON.stringify(state.info))
    }
  },

  // 异步请求接口并存进vuex里
  actions: {
    XITONG_DATA: ({ commit }) => {
      const info = localStorage.getItem('xitong')

      // 当本地数据没有系统配置，则请求接口，若有则直接走本地配置
      if (!info) {
        xitongList().then(res => {
          commit('SET_XITONG', res.rows.length > 0 ? {
            bossName: res.rows[0].bossName,
            logo: res.rows[0].logo,
            logoUrl: res.rows[0].logoUrl,
            logiPcUrl: res.rows[0].logiPcUrl
          } : {})
        }).catch(err => {
        })
      } else {
        commit('SET_XITONG', JSON.parse(info))
      }
    }
  }
}

export default xitong
